package GUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.VolatileImage;
import java.util.Random;
import java.util.Scanner;

public class LoginJFrame extends JFrame implements ActionListener {
    JButton button = new JButton(new ImageIcon("src/GUI/image/login/登录按钮.png"));
    JButton button1 = new JButton(new ImageIcon("src/GUI/image/login/注册按钮.png"));
    String verificationCode = createVerificationCode();
    JLabel text = new JLabel(verificationCode);
    JTextField username = new JTextField(15);
    JPasswordField password = new JPasswordField(15);
    JTextField repassword = new JTextField(5);

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        String usernameText = username.getText();
        String passwordText = password.getText();
        String repasswordText = repassword.getText();
        if(obj == button){
            boolean flag = check(usernameText,passwordText,repasswordText);
            if (!flag) {
                System.out.println("请重新输入");
            }else{
                System.out.println("登入成功");
                new GameJFrame();
            }
        }else if(obj == button1){
            register();
            System.out.println("该用户已成功注册");
        }

    }
    public LoginJFrame(){
        this.setSize(488,430);
        this.setTitle("拼图 登入");
        this.setAlwaysOnTop(true);//设置界面置顶
        this.setLocationRelativeTo(null);//将界面设置为中间
        this.setDefaultCloseOperation(3);
        initButton();
        initImage();
        this.setVisible(true);
    }
    public void initImage(){
        text.setBounds(280,255,100,60);
        this.getContentPane().add(text);

        username.setBounds(175,150,200,25);
        this.add(username);
        password.setBounds(175,213,200,25);
        this.add(password);
        repassword.setBounds(175,273,100,25);
        this.add(repassword);

        JLabel photo2 = new JLabel(new ImageIcon("src/GUI/image/login/用户名.png"));
        photo2.setBounds(100,153,47,17);
        this.add(photo2);

        JLabel photo3 = new JLabel(new ImageIcon("src/GUI/image/login/密码.png"));
        photo3.setBounds(100,213,32,16);
        this.add(photo3);

        JLabel photo4 = new JLabel(new ImageIcon("src/GUI/image/login/验证码.png"));
        photo4.setBounds(100,273,56,21);
        this.add(photo4);

        JLabel photo = new JLabel(new ImageIcon("src/GUI/image/login/background.png"));
        photo.setBounds(0,0,470,390);
        this.add(photo);


        this.getContentPane().repaint();
    }
    public void initButton(){
        button.setBounds(100,325,128,47);
        button1.setBounds(275,325,128,47);

        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button1.setOpaque(false);
        button1.setContentAreaFilled(false);
        button1.setBorderPainted(false);
        button1.setFocusPainted(false);

        button.addActionListener(this);
        button1.addActionListener(this);

        this.add(button);
        this.add(button1);
    }
    private String createVerificationCode() {
        int[] arr = new int[5];
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            arr[i] = r.nextBoolean() ? r.nextInt(65,91) : r.nextInt(97,123);
        }
        arr[4] = r.nextInt(1,10);
        char[] arr1 = new char[4];
        for (int i = 0; i < 4; i++) {
            arr1[i] = (char)arr[i];
        }
        System.out.println(String.valueOf(arr1)+arr[4]);
        return String.valueOf(arr1)+arr[4];
    }
    public boolean check(String username, String password,String repassword){
        if(username == null || password == null || repassword == null){
            System.out.println("账号或密码错误");
            return false;
        }
        if(!repassword.equals(verificationCode)){
            System.out.println("验证码错误");
            return false;
        }
        return App.loginCount(username, password);
    }
    public void register(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的用户名：");
        String s = sc.nextLine();
        System.out.println("请输入你的密码：");
        String s1 = sc.nextLine();
        App.registerPassword(s1, s);
    }
}
